package com.a16.yuejian.yuejian.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.a16.yuejian.yuejian.R;

import java.util.Formatter;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class MaVideoActivity extends AppCompatActivity implements View.OnClickListener,MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener,SeekBar.OnSeekBarChangeListener{
    private VideoView videoView;
    private SeekBar seekBar;
    private String video ="http://10.0.2.2:8080/neu_yuejian201/public/photo/59acea4310b75.mp4";
    private Timer timer;
    private TimerTask task;
    private ImageView back,img;
    private TextView time1,time2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ma_video);
        initView();
        play();
        initEvent();
    }

    private void play() {
        if (!TextUtils.isEmpty(video)){

                Uri uri = Uri.parse(video);
                MediaController mc = new MediaController(this);
                mc.setVisibility(View.INVISIBLE);
                videoView.setMediaController(mc);
                videoView.setVideoURI(uri);
                videoView.requestFocus();
                videoView.start();
        }
    }

    private void initEvent() {
        videoView.setOnPreparedListener(this);
        videoView.setOnCompletionListener(this);
        seekBar.setOnSeekBarChangeListener(this);
        back.setOnClickListener(this);
        img.setOnClickListener(this);
    }

    private void initView() {
        videoView = (VideoView)findViewById(R.id.videoView);
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        back = (ImageView)findViewById(R.id.ma_video_back);
        img = (ImageView)findViewById(R.id.play_or_stop);
        time1 = (TextView)findViewById(R.id.videotime1);
        time2 = (TextView)findViewById(R.id.videotime2);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        videoView.seekTo(0);
        seekBar.setProgress(0);
        img.setImageResource(R.drawable.ic_play);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
       int time = videoView.getDuration();
       time2.setText(getTime(time));

        seekBar.setMax(time);
        mp.start();
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                seekBar.setProgress(videoView.getCurrentPosition());
            }
        };
        timer.schedule(task, 0, 100);
    }

    private String getTime(int time) {
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        int totalSeconds = time / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        mFormatBuilder.setLength(0);
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        time1.setText(getTime(videoView.getCurrentPosition()));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        videoView.seekTo(seekBar.getProgress());
        videoView.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ma_video_back:
                finish();
                break;
            case R.id.play_or_stop:
                if (videoView.isPlaying()){
                    videoView.pause();
                    img.setImageResource(R.drawable.ic_play);
                }else {
                    videoView.start();
                    img.setImageResource(R.drawable.ic_stop);
                }
                break;
        }
    }
}
